package main;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import dao.CommDAO;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Map;
import java.awt.event.ActionEvent;

public class Customer extends JFrame {

	private CommDAO dao = new CommDAO();
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					Customer frame = new Customer();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public Customer() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 625, 418);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("设备id:");
		lblNewLabel.setBounds(155, 36, 54, 15);
		contentPane.add(lblNewLabel);
		
		textField = new JTextField();
		textField.setColumns(10);
		textField.setBounds(208, 33, 139, 21);
		contentPane.add(textField);
		
		JButton btnNewButton = new JButton("查询");
		btnNewButton.setBounds(357, 32, 73, 23);
		contentPane.add(btnNewButton);
		
		JLabel lblNewLabel_1 = new JLabel("设备id:");
		lblNewLabel_1.setBounds(198, 79, 54, 15);
		contentPane.add(lblNewLabel_1);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(244, 76, 139, 21);
		contentPane.add(textField_1);
		
		JLabel lblNewLabel_1_1 = new JLabel("设备名称:");
		lblNewLabel_1_1.setBounds(198, 122, 54, 15);
		contentPane.add(lblNewLabel_1_1);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(244, 119, 139, 21);
		contentPane.add(textField_2);
		
		JLabel lblNewLabel_1_1_1 = new JLabel("设备折旧:");
		lblNewLabel_1_1_1.setBounds(198, 165, 54, 15);
		contentPane.add(lblNewLabel_1_1_1);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(244, 162, 139, 21);
		contentPane.add(textField_3);
		
		JLabel lblNewLabel_1_1_1_1 = new JLabel("设备存放时间：");
		lblNewLabel_1_1_1_1.setBounds(198, 207, 54, 15);
		contentPane.add(lblNewLabel_1_1_1_1);
		
		textField_4 = new JTextField();
		textField_4.setColumns(10);
		textField_4.setBounds(244, 204, 139, 21);
		contentPane.add(textField_4);
		
		JLabel lblNewLabel_1_1_1_1_1 = new JLabel("类型：");
		lblNewLabel_1_1_1_1_1.setBounds(198, 256, 54, 15);
		contentPane.add(lblNewLabel_1_1_1_1_1);
		
		textField_5 = new JTextField();
		textField_5.setColumns(10);
		textField_5.setBounds(244, 253, 139, 21);
		contentPane.add(textField_5);
		
		JButton btnNewButton_1 = new JButton("新增");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String sql = "insert into customer values('" 
						+ textField_1.getText() + "', '"
						+ textField_2.getText()  + "', '"
						+ textField_3.getText()  + "', '"
						+ textField_4.getText()  + "', '"
						+ textField_5.getText()
						+ "')";
				System.out.println(sql);
				dao.commOper(sql);
			}
		});
		btnNewButton_1.setBounds(180, 308, 73, 23);
		contentPane.add(btnNewButton_1);

		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setDefaultValue();
				String sql = "select * from customer where idcard = '" + textField.getText() + "'";
				List<Map<String,Object>> list = dao.select(sql);
				if(list.size() > 0) {
					Map<String, Object> map = list.get(0);
					textField_1.setText(map.get("idcard").toString());
					textField_2.setText(map.get("name").toString());
					textField_3.setText(map.get("phone").toString());
					textField_4.setText(map.get("age").toString());
					textField_5.setText(map.get("sex").toString());
				}
			}
		});
		
		JButton btnNewButton_1_1 = new JButton("修改");
		btnNewButton_1_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String sql = "update customer set "
						+ " name = '" + textField_2.getText() + "', "
						+ " phone = '" + textField_3.getText() + "', "
						+ " age = '" + textField_4.getText() + "', "
						+ " sex = '" + textField_5.getText() + "'"
						+ " where idcard = " + textField_1.getText();
				System.out.println(sql);
				dao.commOper(sql);
			}
		});
		btnNewButton_1_1.setBounds(263, 308, 73, 23);
		contentPane.add(btnNewButton_1_1);
		
		JButton btnNewButton_1_1_1 = new JButton("关闭");
		btnNewButton_1_1_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnNewButton_1_1_1.setBounds(339, 308, 73, 23);
		contentPane.add(btnNewButton_1_1_1);
	}

	public void setDefaultValue() {
		textField_1.setText("");
		textField_2.setText("");
		textField_3.setText("");
		textField_4.setText("");
		textField_5.setText("");
	}
}
